package com.sunday.haowushop.ui.mine.baseinfo;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.event.EventBus;
import com.sunday.common.imageselector.MultiImageSelectorActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.DeviceUtils;
import com.sunday.common.utils.ImageUtils;
import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.utils.UploadUtils;
import com.sunday.haowushop.R;
import com.sunday.haowushop.common.BaseApp;
import com.sunday.haowushop.entity.Member;
import com.sunday.haowushop.event.UpdateBaseInfo;
import com.sunday.haowushop.http.ApiClient;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/5/14.
 */

public class UploadWechatCodeActivity extends BaseActivity {

    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.imgView)
    ImageView imgView;

    private Member member;
    private int appWidth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_wechat_code);
        ButterKnife.bind(this);
        titleView.setText("我的二维码");
        member = BaseApp.getInstance().getMember();
        appWidth=DeviceUtils.getDisplay(mContext).widthPixels;
        initView();

    }

    private void initView() {
        LinearLayout.LayoutParams params= (LinearLayout.LayoutParams) imgView.getLayoutParams();
        params.height= DeviceUtils.getDisplay(mContext).heightPixels*3/5;
        imgView.setLayoutParams(params);
        if (!TextUtils.isEmpty(member.getTwoCode())) {
            Glide.with(mContext)
                    .load(member.getTwoCode())
                    .placeholder(R.mipmap.ic_default)
                    .error(R.mipmap.ic_default)
                    .into(imgView);
        }
    }


    private ArrayList<String> mSelectPath = new ArrayList<>();

    @OnClick(R.id.btn_select_pic)
    void selectPic() {
        intent = new Intent(mContext, MultiImageSelectorActivity.class);
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_SINGLE);
        startActivityForResult(intent, 0x1111);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        mSelectPath.clear();
        mSelectPath = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
        if (mSelectPath != null) {
            String logo = ImageUtils.getCompressImagePath(mSelectPath.get(0));
            Glide.with(mContext)
                    .load(new File(logo))
                    .placeholder(R.mipmap.ic_default)
                    .error(R.mipmap.ic_default)
                    .into(imgView);
            uploadImg(UploadUtils.getRequestBody(logo));
        }
    }

    private void uploadImg(final RequestBody requestBody) {
        showLoadingDialog(0);
        Call<ResultDO<List<String>>> call
                = ApiClient.getApiAdapter().uploadImgs(requestBody);
        call.enqueue(new Callback<ResultDO<List<String>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<String>>> call, Response<ResultDO<List<String>>> response) {
                dissMissDialog();
                if (response.body() == null || isFinish) {
                    return;
                }
                if (response.body() != null && response.body().getCode() == 0) {
                    //ToastUtils.showToast(mContext,"上传成功");
                    List<String> list = new ArrayList<String>();
                    list.addAll(response.body().getResult());
                    update(list.get(0));
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<String>>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, "网络错误");
            }
        });
    }

    private void update(String twoCode) {
        showLoadingDialog(0);
        Call<ResultDO<Member>> call = ApiClient.getApiAdapter().saveEditInfo(
                member.getId(), null, null, null, null, twoCode);
        call.enqueue(new Callback<ResultDO<Member>>() {
            @Override
            public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                dissMissDialog();
                if (response.body() == null || isFinish) {
                    return;
                }
                if (response.body().getResult() != null && response.body().getCode() == 0) {
                    SharePerferenceUtils.getIns(mContext).removeKey("oAuth");
                    SharePerferenceUtils.getIns(mContext).saveOAuth(response.body().getResult());
                    BaseApp.getInstance().setMember(response.body().getResult());
                    initView();
                    EventBus.getDefault().post(new UpdateBaseInfo());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Member>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, "网络错误");
            }
        });
    }

}
